﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BI.CF.Model;
using BI.CF.PageList;
using BI.CF.IDAL;
using System.Data.SqlClient;
using System.Data;
namespace BI.CF.DAL
{
    public class PRDetailsDAL : PRDetailsIDAL
    {
        /// <summary>
        /// 显示平台收益
        /// </summary>
        /// <param name="pageindex">当前页</param>
        /// <param name="pagesize">显示条数</param>
        /// <param name="time">入账时间</param>
        /// <returns></returns>
        public Pager<PRDetailsModel> PRDetails_Show(int pageindex, int pagesize, string startime, string endtime)
        {
            SqlParameter[] parameter ={
                                         new SqlParameter ("@PageIndex",SqlDbType.Int),
                                         new SqlParameter ("@PageSize",SqlDbType.Int),
                                         new SqlParameter("@StarTime",SqlDbType.VarChar),
                                         new SqlParameter("@EndTime",SqlDbType.VarChar)
                                     };
            parameter[0].Value = pageindex;
            parameter[1].Value = pagesize;
            parameter[2].Value = startime.ToString();
            parameter[3].Value = endtime.ToString();
            SqlDataReader read = DbHelperSQL.RunProcedure("PROC_PRDetails_Show", parameter);
            Pager<PRDetailsModel> list = new Pager<PRDetailsModel>();
            List<PRDetailsModel> mlist = new List<PRDetailsModel>();
            
            while (read.Read())
            {
                PRDetailsModel m = new PRDetailsModel();
                
                m.PRDetailsId = Convert.ToInt32(read["PRDetailsId"]);
                m.PRDetailsInMoney = Convert.ToDecimal(read["PRDetailsInMoney"]);
                m.PRDetailsInDatetime = Convert.ToDateTime(read["PRDetailsInDatetime"]);
                m.PRDetailsInTerm = read["PRDetailsInTerm"].ToString();
                m.PRDetailsCount = 0;
                mlist.Add(m);
            }
            //一  连接数据源
            SqlConnection conn = new SqlConnection(" Data Source=169.254.155.64;Initial Catalog=CF_DB;Persist Security Info=True;User ID=sa;Pwd=123456");
            conn.Open();
            string sqlcount = "select PRDetailsId   from PRDetails WHERE 1=1 ";
            //判断开始时间与结束时间是否为空,如果两个时间都不空的情况下则查询开始时间与结束时间之间的数据
            if (startime != "" && endtime != "" )
            {
                sqlcount += "AND PRDetailsInDatetime >'"+startime+"' AND PRDetailsInDatetime  <'"+endtime+"' ";
            }
            //判断开始时间与结束时间是否为空,如果开始时间不为空但结束时间为空的情况下则查询开始时间往后的数据
            else if (startime != "" && endtime == ""  )
            {
                sqlcount += " AND PRDetailsInDatetime > '" + startime + "'";
            }
            //判断开始时间与结束时间是否为空,如果开始时间为空但结束时间不为空的情况下则查询结束时间之前的数据
            else if (startime == "" && endtime != "" )
            {
                sqlcount += "  AND PRDetailsInDatetime < '"+endtime+"'";
            }
            SqlCommand cmd = new SqlCommand(sqlcount, conn);//要执行的sql语句  选择数据源
            SqlDataReader sread = cmd.ExecuteReader();
            int dcount = 0;
            while (sread.Read())
            {
                dcount++;   
            }
            conn.Close();
          
            list.Data = mlist;
            list.PageIndex = pageindex;//当前页
            list.PageSize = pagesize;//显示条数
            //总页数
            if (dcount % pagesize == 0)
            {
                list.PageCount = dcount / pagesize;
            }
            else
            {
                list.PageCount = (dcount / pagesize) + 1;
            }
            return list;
        }

        /// <summary>
        /// 平台的总收益
        /// </summary>
        /// <returns></returns>
        public decimal PintTai_MoneySum()
        {
            string sqlcountq = "select SUM(PRDetailsInMoney) from PRDetails";
            decimal SumCount=Convert.ToDecimal(DbHelperSQL.GetSingle(sqlcountq));
            return SumCount;
        }
    }
}
